package com.example.archermind.myapplication;

import android.androidcontrol.IAndroidControlService;
import android.androidcontrol.IChangeStatusBarCallback;
import android.androidcontrol.IDialPhoneNumberBarCallback;
import android.androidcontrol.ISystemSwitchCallback;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.network.INetworkService;
import android.network.INetworkServiceCallback;
import android.notification.INotificationGatewayCallback;
import android.notification.INotificationGatewayService;
import android.online.ICNSOnLineCallback;
import android.online.ICNSOnLineService;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.poi.IPoiService;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import com.cns.android.gateway.IGatewaySystemCallback;
import com.cns.android.gateway.IGatewaySystemService;

import java.util.List;

import butterknife.Unbinder;

public class BaseActivity extends AppCompatActivity {

    protected String TAG = getClass().toString();
    protected Unbinder unbinder;
    private static INotificationGatewayService notificationGatewayService;
    private static IGatewaySystemService gatewaySystemService;
    private static IAndroidControlService androidControlService;
    private static INetworkService networkService;
    private static ICNSOnLineService cnsOnLineService;
    private static IPoiService poiService;

    private ChangeStatusBarCallback changeStatusBarCallback;
    private GatewaySystemCallback gatewaySystemCallback;
    private DialPhoneNumberBarCallback dialPhoneNumberBarCallback;
    private NotificationGatewayCallback notificationGatewayCallback;
    private NetworkServiceCallback networkServiceCallback;
    private SystemSwitchCallback systemSwitchCallback;
    private CNSOnLineCallback cnsOnLineCallback;

    private Handler mHanlder;

    public final static int NOTIFICATIONCALLBACK = 100;
    public final static int REQUESTENTERTAINMENTCALLBACK = 200;
    public final static int BACKGROUNDMODECALLBACK = 300;
    public final static int GATEWAYSYSTEMCALLBACK = 400;
    public final static int CHANGESTATUSBARCALLBACK = 500;
    public final static int SYSTEMSWITCHCALLBACK = 600;
    public final static int DIALPHONENUMBERBARCALLBACK = 700;
    public final static int NETWORKSERVICECALLBACK = 800;
    public final static int CNSONLINECALLBACK = 900;






//    protected static INotificationGatewayCallback notificationGatewayCallback;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    }

    protected void setHandler(Handler handler){
        this.mHanlder = handler;
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        notificationGatewayService = null;
        gatewaySystemService = null;
        androidControlService = null;
    }

    protected INotificationGatewayService getNotificationService(){
        if(notificationGatewayService==null){
            Log.d(TAG,"init NotificationService");
            IBinder binder = ServiceManager.getService(INotificationGatewayService.SERVICE_NAME);
            notificationGatewayService = INotificationGatewayService.Stub.asInterface(binder);
            notificationGatewayCallback = new NotificationGatewayCallback(this);
        }
        return notificationGatewayService;
    }

    protected  IGatewaySystemService getGatewaySystemService(){
        if(gatewaySystemService==null){
            IBinder binder = ServiceManager.getService(IGatewaySystemService.SERVICE_NAME);
            gatewaySystemService = IGatewaySystemService.Stub.asInterface(binder);
            gatewaySystemCallback = new GatewaySystemCallback(this);
        }
        return gatewaySystemService;
    }

    protected IAndroidControlService getAndroidControlService(){
        if(androidControlService==null){
            IBinder binder = ServiceManager.getService(IAndroidControlService.SERVICE_NAME);
            androidControlService = IAndroidControlService.Stub.asInterface(binder);
            changeStatusBarCallback = new ChangeStatusBarCallback(this);
            dialPhoneNumberBarCallback = new DialPhoneNumberBarCallback(this);
            systemSwitchCallback = new SystemSwitchCallback(this);
        }
        return androidControlService;
    }

    protected INetworkService getNetworkService() {
        if(networkService==null){
            IBinder binder = ServiceManager.getService(INetworkService.SERVICE_NAME);
            networkService = INetworkService.Stub.asInterface(binder);
            networkServiceCallback = new NetworkServiceCallback(this);
        }
        return networkService;
    }

    protected ICNSOnLineService getCnsOnLineService(){
        if(cnsOnLineService==null){
            IBinder binder = ServiceManager.getService(ICNSOnLineService.SERVICE_NAME);
            cnsOnLineService = ICNSOnLineService.Stub.asInterface(binder);
            cnsOnLineCallback = new CNSOnLineCallback(this);
        }
        return cnsOnLineService;

    }

    protected IPoiService getPoiService(){
        if(poiService==null){
            IBinder binder = ServiceManager.getService(IPoiService.SERVICE_NAME);
            poiService = IPoiService.Stub.asInterface(binder);
        }
        return poiService;
    }

    protected class NotificationGatewayCallback extends INotificationGatewayCallback.Stub {

        private Context mContext;


        /**
         * 构造函数
         *
         * @param context
         */
        public NotificationGatewayCallback(Context context) {
            this.mContext = context;
        }

        @Override
        public void onClickNotification(String notificationid) throws RemoteException {
            Log.d(TAG, "onClickNotification");
            Message message = mHanlder.obtainMessage();
            Bundle bundle = message.getData();
            bundle.putString("notificationid",notificationid);
            message.setData(bundle);
            message.what = NOTIFICATIONCALLBACK+0;
            mHanlder.sendMessage(message);
//            doStartApplicationWithPackageName("com.cns.android.notemanager");
        }


        @Override
        public void onDelNotification(String notificationid) throws RemoteException {
            Log.d(TAG, "onDelNotification");
            Message message = mHanlder.obtainMessage();
            Bundle bundle = message.getData();
            bundle.putString("notificationid",notificationid);
            message.setData(bundle);
            message.what = NOTIFICATIONCALLBACK+1;
            mHanlder.sendMessage(message);
        }


    }

    protected class GatewaySystemCallback extends IGatewaySystemCallback.Stub{
        private Context mContext;

        public GatewaySystemCallback(Context context) {
            this.mContext = context;
        }


        @Override
        public void onOpenAndroidApp(String action, int result) throws RemoteException {
            Log.d(TAG, "onOpenAndroidApp");
            Message message = mHanlder.obtainMessage();
            Bundle bundle = message.getData();
            bundle.putString("action",action);
            bundle.putInt("result",result);
            message.setData(bundle);
            message.what = GATEWAYSYSTEMCALLBACK+1;
            mHanlder.sendMessage(message);
        }

        @Override
        public void onCloseAndroidApp(String action, int result) throws RemoteException {
            Log.d(TAG, "onCloseAndroidApp");
            Message message = mHanlder.obtainMessage();
            Bundle bundle = message.getData();
            bundle.putString("action",action);
            bundle.putInt("result",result);
            message.setData(bundle);
            message.what = GATEWAYSYSTEMCALLBACK+1;
            mHanlder.sendMessage(message);
        }
    }

    protected class ChangeStatusBarCallback extends IChangeStatusBarCallback.Stub{
        private Context mContext;

        public ChangeStatusBarCallback(Context context) {
            this.mContext = context;
        }

        @Override
        public void onStatusBarChangeCallback(int mode) throws RemoteException {
            Log.d(TAG,"ChangeStatusBarCallback onStatusBarChangeCallback mode:"+mode);
            Message message = mHanlder.obtainMessage();
            Bundle bundle = message.getData();
            bundle.putInt("mode",mode);
            message.setData(bundle);
            message.what = CHANGESTATUSBARCALLBACK+0;
            mHanlder.sendMessage(message);

        }
    }

    protected class DialPhoneNumberBarCallback extends IDialPhoneNumberBarCallback.Stub{

        private Context mContext;

        public DialPhoneNumberBarCallback(Context context) {
            this.mContext = context;
        }
        @Override
        public void onDialPhoneNumberBarCallback(int result) throws RemoteException {
            Log.d(TAG,"DialPhoneNumberBarCallback onDialPhoneNumberBarCallback reslut:"+result);
            Message message = mHanlder.obtainMessage();
            Bundle bundle = message.getData();
            bundle.putInt("reslut",result);
            message.setData(bundle);
            message.what = DIALPHONENUMBERBARCALLBACK+0;
            mHanlder.sendMessage(message);
        }
    }

    protected class NetworkServiceCallback extends INetworkServiceCallback.Stub{
        private Context mContext;

        public NetworkServiceCallback(Context context) {
            this.mContext = context;
        }

        @Override
        public void onMobile1NetworkStatusChange(int status, String type, int intensity) throws RemoteException {
            Log.d(TAG,"NetworkServiceCallback onMobile1NetworkStatusChange status:"+status+"type:"+type+"intensity:"+intensity);
            Message message = mHanlder.obtainMessage();
            Bundle bundle = message.getData();
            bundle.putInt("status",status);
            bundle.putString("type",type);
            bundle.putInt("intensity",intensity);
            message.setData(bundle);
            message.what = NETWORKSERVICECALLBACK+0;
            mHanlder.sendMessage(message);
        }

        @Override
        public void onMobile2NetworkStatusChange(int status, String type, int intensity) throws RemoteException {
            Log.d(TAG,"NetworkServiceCallback onMobile2NetworkStatusChange reslut:"+status+"type:"+type+"intensity:"+intensity);
            Message message = mHanlder.obtainMessage();
            Bundle bundle = message.getData();
            bundle.putInt("status",status);
            bundle.putString("type",type);
            bundle.putInt("intensity",intensity);
            message.setData(bundle);
            message.what = NETWORKSERVICECALLBACK+1;
            mHanlder.sendMessage(message);
        }

        @Override
        public void onWiFiNetworkStatusChange(int status, String type, int intensity) throws RemoteException {
            Log.d(TAG,"NetworkServiceCallback onWiFiNetworkStatusChange reslut:"+status+"type:"+type+"intensity:"+intensity);
            Message message = mHanlder.obtainMessage();
            Bundle bundle = message.getData();
            bundle.putInt("status",status);
            bundle.putString("type",type);
            bundle.putInt("intensity",intensity);
            message.setData(bundle);
            message.what = NETWORKSERVICECALLBACK+2;
            mHanlder.sendMessage(message);
        }
    }

    protected class SystemSwitchCallback extends ISystemSwitchCallback.Stub{
        private Context mContext;

        public SystemSwitchCallback(Context context) {
            this.mContext = context;
        }

        @Override
        public void onSystemSwitch(int system) throws RemoteException {
            Log.d(TAG,"SystemSwitchCallback onSystemSwitch system:"+system);
            Message message = mHanlder.obtainMessage();
            Bundle bundle = message.getData();
            bundle.putInt("system",system);
            message.setData(bundle);
            message.what = SYSTEMSWITCHCALLBACK+0;
            mHanlder.sendMessage(message);
        }
    }

    protected class CNSOnLineCallback extends ICNSOnLineCallback.Stub{
        private Context mContext;

        public CNSOnLineCallback(Context context) {
            this.mContext = context;
        }

        @Override
        public void onBrandChanged(String brand) throws RemoteException {
            Log.d(TAG,"CNSOnLineCallback onBrandChanged brand:"+brand);
            Message message = mHanlder.obtainMessage();
            Bundle bundle = message.getData();
            bundle.putString("brand",brand);
            message.setData(bundle);
            message.what = CNSONLINECALLBACK+0;
            mHanlder.sendMessage(message);
        }

        @Override
        public void onOnlineServiceChanged(boolean isonline) throws RemoteException {
            Log.d(TAG,"CNSOnLineCallback onBrandChanged isonline:"+isonline);
            Message message = mHanlder.obtainMessage();
            Bundle bundle = message.getData();
            bundle.putBoolean("isonline",isonline);
            message.setData(bundle);
            message.what = CNSONLINECALLBACK+1;
            mHanlder.sendMessage(message);
        }

        @Override
        public void onManufacturerChanged(int type) throws RemoteException {
            Log.d(TAG,"CNSOnLineCallback onManufacturerChanged isonline:"+type);
            Message message = mHanlder.obtainMessage();
            Bundle bundle = message.getData();
            bundle.putInt("type",type);
            message.setData(bundle);
            message.what = CNSONLINECALLBACK+2;
            mHanlder.sendMessage(message);
        }

        @Override
        public void onVariantChanged(int variant) throws RemoteException {
            Log.d(TAG,"CNSOnLineCallback onVariantChanged variant:"+variant);
            Message message = mHanlder.obtainMessage();
            Bundle bundle = message.getData();
            bundle.putInt("variant",variant);
            message.setData(bundle);
            message.what = CNSONLINECALLBACK+3;
            mHanlder.sendMessage(message);
        }
    }


    protected void startIntent(Context context,Class tClass){
        Intent intent = new Intent(context,tClass);
        startActivity(intent);
    }

    protected void doStartApplicationWithPackageName(String packagename) {

        // 通过包名获取此APP详细信息，包括Activities、services、versioncode、name等等
        PackageInfo packageinfo = null;
        try {
            packageinfo = getPackageManager().getPackageInfo(packagename, 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        if (packageinfo == null) {
            return;
        }

        // 创建一个类别为CATEGORY_LAUNCHER的该包名的Intent
        Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null);
        resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        resolveIntent.setPackage(packageinfo.packageName);

        // 通过getPackageManager()的queryIntentActivities方法遍历
        List<ResolveInfo> resolveinfoList = getPackageManager()
                .queryIntentActivities(resolveIntent, 0);

        ResolveInfo resolveinfo = resolveinfoList.iterator().next();
        if (resolveinfo != null) {
            // packagename = 参数packname
            String packageName = resolveinfo.activityInfo.packageName;
            // 这个就是我们要找的该APP的LAUNCHER的Activity[组织形式：packagename.mainActivityname]
            String className = resolveinfo.activityInfo.name;
            // LAUNCHER Intent
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);

            // 设置ComponentName参数1:packagename参数2:MainActivity路径
            ComponentName cn = new ComponentName(packageName, className);

            intent.setComponent(cn);
            startActivity(intent);
        }
    }


    protected static INotificationGatewayService getNotificationGatewayService() {
        return notificationGatewayService;
    }

    protected ChangeStatusBarCallback getChangeStatusBarCallback() {
        return changeStatusBarCallback;
    }


    protected GatewaySystemCallback getGatewaySystemCallback() {
        return gatewaySystemCallback;
    }

    protected DialPhoneNumberBarCallback getDialPhoneNumberBarCallback() {
        if(dialPhoneNumberBarCallback==null)
            dialPhoneNumberBarCallback = new DialPhoneNumberBarCallback(this);
        return dialPhoneNumberBarCallback;
    }

    protected NotificationGatewayCallback getNotificationGatewayCallback() {
        if(notificationGatewayCallback==null) {
            notificationGatewayCallback = new NotificationGatewayCallback(this);
        }
        return notificationGatewayCallback;
    }

    protected NetworkServiceCallback getNetworkServiceCallback() {
        if(networkServiceCallback==null)
            networkServiceCallback = new NetworkServiceCallback(this);
        return networkServiceCallback;
    }

    public SystemSwitchCallback getSystemSwitchCallback() {
        if(systemSwitchCallback==null) {
            systemSwitchCallback = new SystemSwitchCallback(this);
        }
        return systemSwitchCallback;
    }

    public CNSOnLineCallback getCnsOnLineCallback(){
        if(cnsOnLineCallback==null){
            cnsOnLineCallback = new CNSOnLineCallback(this);
        }
        return cnsOnLineCallback;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);//加载menu文件到布局
        return super.onCreateOptionsMenu(menu);
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.item_back:
                finish();
                break;
            default:break;
        }
        return super.onOptionsItemSelected(item);
    }
}
